package com.intelligent.health.setmeal.controller;

import com.intelligent.health.setmeal.dto.CheckGroupDto;
import com.intelligent.health.setmeal.pojo.CheckGroup;
import com.intelligent.health.setmeal.pojo.CheckItem;
import com.intelligent.health.setmeal.service.CheckGroupService;
import com.intelligent.health.setmeal.vo.CheckGroupVo;
import entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/13/22:35
 * @Description: 检查组
 */
@Slf4j
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 分页查询检查组
     *
     * @param checkGroupDto 查询条件
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @return 检查组集合
     */
    @PostMapping("/all/{pageNum}/{pageSize}")
    public R queryCheckGroupsByPage(@RequestBody CheckGroupDto checkGroupDto,
                                    @PathVariable("pageNum") Integer pageNum,
                                    @PathVariable("pageSize") Integer pageSize) {
        PageResult<CheckGroupVo> checkGroups = checkGroupService.queryCheckGroupsByPage(checkGroupDto, pageNum, pageSize);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("checkGroups",checkGroups);
    }


    /**
     * 根据checkGroupId 查询checkGroup
     *
     * @param checkGroupId 检查组id
     * @return 检查组信息
     */
    @GetMapping("/{checkGroupId}")
    public R queryCheckGroupById(@PathVariable("checkGroupId") Integer checkGroupId) {
        CheckGroupVo checkGroupVo = checkGroupService.queryCheckGroupById(checkGroupId);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("checkGroupVo",checkGroupVo);
    }

    /**
     * 新增检查组
     *
     * @param hashMap key:
     *                检查组: checkGroup
     *                检查项ids: checkItemIds
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('CHECKGROUP_ADD')")
    @PostMapping("/add")
    public R addCheckGroup(@RequestBody HashMap<String, Object> hashMap) {
        try {
            checkGroupService.addCheckGroup(hashMap);
        } catch (Exception e) {
            log.error("新增检查组失败,请勿重复添加: {}", hashMap);
            return R.setResult(ResultCodeEnum.INSERT_ERROR);
        }
        log.info("新增检查组成功: {}", hashMap);
        return R.setResult(ResultCodeEnum.INSERT_SUCCESS);
    }

    /**
     * 更新检查组
     *
     * @param hashMap key:
     *                检查组: checkGroup
     *                检查项ids: checkItemIds
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('CHECKGROUP_EDIT')")
    @PutMapping("/edit")
    public R editCheckGroup(@RequestBody HashMap<String, Object> hashMap) {
        try {
            checkGroupService.editCheckGroup(hashMap);
        } catch (Exception e) {
            log.error("找不到该记录，更新检查组失败: {}", hashMap);
            return R.setResult(ResultCodeEnum.UPDATE_ERROR);
        }
        log.info("更新检查组成功: {}", hashMap);
        return R.setResult(ResultCodeEnum.UPDATE_SUCCESS);
    }

    /**
     * 逻辑删除检查组
     *
     * @param checkGroupId 检查组id
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('CHECKGROUP_DELETE')")
    @DeleteMapping("/del/{checkGroupId}")
    public R delCheckGroup(@PathVariable("checkGroupId") Integer checkGroupId) {
        try {
            checkGroupService.delCheckGroupById(checkGroupId);
        } catch (Exception e) {
            log.error("删除检查组失败: {}", checkGroupId);
            return R.setResult(ResultCodeEnum.DELETE_ERROR);
        }
        log.info("删除检查组成功: {}", checkGroupId);
        return R.setResult(ResultCodeEnum.DELETE_SUCCESS);
    }

    /**
     * 批量删除
     *
     * @param checkGroupIds 检查组ids
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('CHECKGROUP_DELETE')")
    @DeleteMapping("/delMore")
    public R delMoreCheckGroup(@RequestBody ArrayList<Integer> checkGroupIds) {
        try {
            checkGroupService.delBatchCheckGroup(checkGroupIds);
        } catch (Exception e) {
            log.error("批量删除检查组失败: {}", checkGroupIds);
            return R.setResult(ResultCodeEnum.DELETE_ERROR);
        }
        log.info("批量删除检查组成功: {}", checkGroupIds);
        return R.setResult(ResultCodeEnum.DELETE_SUCCESS);
    }

    /**
     * 查询检查组数据 for export Excel
     *
     * @param hashMap key: checkGroupDto value: CheckGroupDto(必须)
     *                key: pageInfo value: PageInfo(非必须)
     * @return 检查组集合
     */
    @PostMapping("/all/excel")
    public R queryCheckGroupsForExcel(@RequestBody HashMap<String, Object> hashMap) {
        if (hashMap.get("checkGroupDto") == null) {
            return R.setResult(ResultCodeEnum.QUERY_ERROR);
        }
        List<CheckGroup> checkGroups = checkGroupService.queryCheckGroups(hashMap);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("checkGroups",checkGroups);
    }

    /**
     * 根据检查组id查询检查项集合
     * @param checkGroupId 检查组id
     * @return 检查项信息集合
     */
    @GetMapping("/checkItems/{checkGroupId}")
    public R queryCheckItemsByGid(@PathVariable("checkGroupId") Integer checkGroupId){
        List<CheckItem> checkItems = checkGroupService.queryCheckItemsByGid(checkGroupId);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("checkItems",checkItems);
    }
}
